/*
@title: Spridle
@description: Spridle is a retro-themed game inspired by Wordle, but uses sequences of 5 numbers instead of letters. Players move a selection arrow to choose numbers, attempting to match a hidden sequence, with feedback given through green and red boxes to indicate correct and incorrect numbers.
@author: Duclings
@tags: ['retro']
@addedOn: 2024-06-23
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started

*/

const underscore = "u"
const zero = "0"
const one = "1"
const two = "2"
const three = "3"
const four = "4"
const five = "f"
const zerounderscore = "5"
const oneunderscore = "6"
const twounderscore = "7"
const threeunderscore = "8"
const fourunderscore = "9"
const fiveunderscore = "t"
const arrow = "a"
const crossBox = "c"
const correct = "g"
const wrong = "r"



let guess = 0
let currentGuess = 1
let currentDigit = 0
let answer = [];
let guesses = [];
let correctDigits = 0;

let startup = true;
let win = 0;

setLegend(
  [underscore, bitmap`
................
................
................
................
................
................
................
................
................
................
................
................
................
................
................
.00000000000000.`],
  [zero, bitmap`
................
................
................
.....000000.....
....0......0....
....0......0....
....0......0....
....0......0....
....0......0....
....0......0....
....0......0....
....0......0....
.....000000.....
................
................
................`],
  [one, bitmap`
................
................
................
........0.......
.......00.......
......0.0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
......00000.....
................
................
................`],
  [two, bitmap`
................
................
................
......000.......
.....0...0......
..........0.....
..........0.....
..........0.....
.........0......
.......00.......
......0.........
.....0..........
.....000000.....
................
................
................`],
  [three, bitmap`
................
................
................
.....000000.....
....0......0....
...........0....
...........0....
.......0000.....
...........0....
...........0....
...........0....
....0......0....
.....000000.....
................
................
................`],
  [four, bitmap`
................
................
................
.........0......
........00......
.......0.0......
......0..0......
.....0...0......
....0....0......
...0.....0......
...0000000......
.........0......
.........0......
................
................
................`],
  [five, bitmap`
................
................
................
....00000000....
....0...........
....0...........
....0...........
.....000000.....
...........0....
...........0....
...........0....
...........0....
.....000000.....
................
................
................`],
  [zerounderscore, bitmap`
................
................
................
.....000000.....
....0......0....
....0......0....
....0......0....
....0......0....
....0......0....
....0......0....
....0......0....
....0......0....
.....000000.....
................
................
.00000000000000.`],
  [oneunderscore, bitmap`
................
................
................
........0.......
.......00.......
......0.0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
......00000.....
................
................
.00000000000000.`],
  [twounderscore, bitmap`
................
................
................
......000.......
.....0...0......
..........0.....
..........0.....
..........0.....
.........0......
.......00.......
......0.........
.....0..........
.....000000.....
................
................
.00000000000000.`],
  [threeunderscore, bitmap`
................
................
................
.....000000.....
....0......0....
...........0....
...........0....
.......0000.....
...........0....
...........0....
...........0....
....0......0....
.....000000.....
................
................
.00000000000000.`],
  [fourunderscore, bitmap`
................
................
................
.........0......
........00......
.......0.0......
......0..0......
.....0...0......
....0....0......
...0.....0......
...0000000......
.........0......
.........0......
................
................
.00000000000000.`],
  [fiveunderscore, bitmap`
................
................
................
....00000000....
....0...........
....0...........
....0...........
.....000000.....
...........0....
...........0....
...........0....
...........0....
.....000000.....
................
................
.00000000000000.`],
  [arrow, bitmap`
................
................
......33333.....
......33333.....
......33333.....
......33333.....
......33333.....
......33333.....
......33333.....
...33333333333..
....333333333...
.....3333333....
......33333.....
.......333......
........3.......
................`],
  [crossBox, bitmap`
.33333333333333.
33............33
3.3..........3.3
3..3........3..3
3...3......3...3
3....3....3....3
3.....3..3.....3
3......33......3
3......33......3
3.....3..3.....3
3....3....3....3
3...3......3...3
3..3........3..3
3.3..........333
33............33
.33333333333333.`],
  [correct, bitmap`
4444444444444444
4444444444444444
44............44
44............44
44............44
44............44
44............44
44............44
44............44
44............44
44............44
44............44
44............44
44............44
4444444444444444
4..............4`],
  [wrong, bitmap`
3333333333333333
3333333333333333
33............33
33............33
33............33
33............33
33............33
33............33
33............33
33............33
33............33
33............33
33............33
33............33
3333333333333333
3..............3`],
)



addText("SPRIDLE", {
  x: 6,
  y: 0,
  color: color`0`
})

setSolids([])

let level = 0
const levels = [
  map`
.....
uuuuu
uuuuu
uuuuu
uuuuu
a....
01234`,
  map`
.uuuuu.
.uuuuu.
.uuuuu.
.uuuuu.
.uuuuu.
.a.....
.01234.`,
  map`
.....
uuuuu
uuuuu
uuuuu
a....
01234`,
  map`
.....
uuuuu
uuuuu
uuuuu
a....
01234`

]

var currentLevel = levels[level];

answer = [];

if (startup) {
  for (let i = 0; i < 5; i++) {
    guesses[i] = 10;
  }



  for (let i = 0; i < 5; i++) {
    answer[i] = Math.floor(Math.random() * 5)

    while (answer[1] == answer[0]) {
      answer[1] = Math.floor(Math.random() * 5)
    }
    while (answer[2] == answer[1] || answer[2] == answer[0]) {
      answer[2] = Math.floor(Math.random() * 5)
    }
    while (answer[3] == answer[2] || answer[3] == answer[1] || answer[3] == answer[0]) {
      answer[3] = Math.floor(Math.random() * 5)
    }
    while (answer[4] == answer[0] || answer[4] == answer[1] || answer[4] == answer[2] || answer[4] == answer[3]) {
      answer[4] = Math.floor(Math.random() * 5)
    }
  }

  startup = false;
}

setMap(levels[level])

setPushables({
  [arrow]: []
});



onInput("a", () => {
  getFirst(arrow).x -= 1;
  if (guess > 0) {
    guess--;
  }
});

onInput("d", () => {
  getFirst(arrow).x += 1;
  if (guess < 4) {
    guess++;
  }
});

onInput("j", () => {
  const currentLevel = levels[0]; // get the original map of the level

  // make sure the level exists before we load it
  if (currentLevel !== undefined) {
    clearText("");
    setMap(currentLevel);
  }
  clearText()
  addText("SPRIGLE", {
    x: 6,
    y: 0,
    color: color`0`

  })

  guess = 0
  currentGuess = 1
  currentDigit = 0
  correctDigits = 0
  win = 0;
  for (let i = 0; i < 5; i++) {
    answer[i] = Math.floor(Math.random() * 5)

    while (answer[1] == answer[0]) {
      answer[1] = Math.floor(Math.random() * 5)
    }
    while (answer[2] == answer[1] || answer[2] == answer[0]) {
      answer[2] = Math.floor(Math.random() * 5)
    }
    while (answer[3] == answer[2] || answer[3] == answer[1] || answer[3] == answer[0]) {
      answer[3] = Math.floor(Math.random() * 5)
    }
    while (answer[4] == answer[0] || answer[4] == answer[1] || answer[4] == answer[2] || answer[4] == answer[3]) {
      answer[4] = Math.floor(Math.random() * 5)
    }
  }
});

onInput("w", () => {
  if (currentGuess < 6) {
    addSprite(guess, 6, crossBox);
    switch (currentDigit) {
      case 0:
        guesses[0] = guess;
        break;
      case 1:
        guesses[1] = guess;
        break;
      case 2:
        guesses[2] = guess;
        break;
      case 3:
        guesses[3] = guess;
        break;
      case 4:
        guesses[4] = guess;
        break;
    }
    switch (guess) {
      case 0:
        addSprite(currentDigit, currentGuess, zerounderscore);
        currentDigit++;
        break;
      case 1:
        addSprite(currentDigit, currentGuess, oneunderscore);
        currentDigit++;
        break;
      case 2:
        addSprite(currentDigit, currentGuess, twounderscore);
        currentDigit++;
        break;
      case 3:
        addSprite(currentDigit, currentGuess, threeunderscore);
        currentDigit++;
        break;
      case 4:
        addSprite(currentDigit, currentGuess, fourunderscore);
        currentDigit++;
        break;
    }
  }

});

onInput("s", () => {

  currentDigit = 0;
  for (let i = 0; i < 5; i++) {
    if (guesses[i] == answer[i]) {
      addSprite(i, currentGuess, correct);
      correctDigits++;
    } else if (guesses[i] != 10) {
      addSprite(i, currentGuess, wrong);

    }
  }
  currentGuess++;
  clearGuesses();
  for (let i = 0; i < 5; i++) {
    guesses[i] = 10;
  }
  if (correctDigits == 5) {
    win = 1;
  }
  correctDigits = 0;

});

function clearGuesses() {
  for (let i = 0; i < 5; i++) {
    clearTile(i, 6)
  }
  addSprite(0, 6, zero)
  addSprite(1, 6, one)
  addSprite(2, 6, two)
  addSprite(3, 6, three)
  addSprite(4, 6, four)
}



afterInput(() => {
  if (currentDigit == 5) {

    currentDigit = 0;
    for (let i = 0; i < 5; i++) {
      if (guesses[i] == answer[i]) {
        addSprite(i, currentGuess, correct);
        correctDigits++;
      } else if (guesses[i] != 10) {
        addSprite(i, currentGuess, wrong);

      }
    }
    currentGuess++;
    clearGuesses();
    for (let i = 0; i < 5; i++) {
      guesses[i] = 10;
    }
    if (correctDigits == 5) {
      win = 1;
    }
    correctDigits = 0;

  }
  if (currentGuess == 5) {
    if (win == 0) {
      for (let i = 0; i < 5; i++) {
        for (let j = 0; j < 7; j++) {
          addSprite(i, j, crossBox);
        }
      }
      addText("YOU LOSE!", {
        x: 6,
        y: 0,
        color: color`5`
      })
      addText("Press J ", {
        x: 5,
        y: 2,
        color: color`5`
      })
      addText("to restart", {
        x: 5,
        y: 4,
        color: color`5`
      })
    }

  }
  if (win == 1) {
    currentGuess == 100;
    addText("YOU WIN!", {
      x: 6,
      y: 0,
      color: color`0`
    })
    addText("Press J", {
      x: 5,
      y: 2,
      color: color`5`
    })
    addText("To restart", {
      x: 5,
      y: 4,
      color: color`5`
    })
  }
})